package com.example.votemanage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.votemanage.config.HttpClientUtil;
import com.example.votemanage.config.WxApiConfig;
import com.example.votemanage.entity.VoteUser;
import com.example.votemanage.service.impl.VoteUserServiceImpl;
import com.fasterxml.jackson.databind.util.JSONPObject;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xingcheng
 * @since 2024-06-06
 */
@Controller
public class VoteUserController {
    @Autowired
    VoteUserServiceImpl voteUserService;
    @RequestMapping("/login")
    public String login(String userid, String password, HttpSession session,HttpServletRequest request) {
        VoteUser voteUser=voteUserService.selectByNameAndPassword(userid,password);
        if(voteUser!=null){
            if(voteUser.getVuStatus()==1){
                request.setAttribute("msg","该用户已被禁用，请联系管理员");
                return "/login";
            }else {
                session.setAttribute("user", voteUser);
            }
        }else {
//            request.setAttribute("msg","用户名或密码错误");
            return "/login";
        }
        return "redirect:/index";
    }
    @PostMapping("/register")
    public String register(VoteUser voteUser){
            voteUser.setVuStatus(2);
            voteUser.setVuVersion(1);
            boolean saveResult = voteUserService.save(voteUser);
            if (saveResult) {
                return "redirect:login";
            } else {
                return "register";
            }
    }
    @RequestMapping("/register")
    public String register(){
        return "register";
    }
    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/login";
    }

    @RequestMapping("test")
    @ResponseBody
    @CrossOrigin
    public Map<String,Object> test(String username,String password){
        QueryWrapper<VoteUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("VU_USER_NAME",username);
        queryWrapper.eq("VU_PASSWORD",password);
        VoteUser voteUser=voteUserService.selectByNameAndPassword(username,password);
        Map map=new HashMap<>();
        if(voteUser!=null){
            if(voteUser.getVuStatus()==1){
                map.put("errorMsg","该用户已被禁用，请联系管理员");

            }else {
                map.put("logintype",true);
                map.put("userinfo", voteUser);
            }
        }else {

            map.put("errorMsg","用户名或密码错误");

        }

        System.out.println(username);
        System.out.println(password);
        return map;
    }
//    /**
//     * 微信登录
//     * @param code 登录时获取的 code，可通过wx.login获取
//     * @return
//     */
//    @RequestMapping("wxlogin")
//    public VoteUser wxlogin(String code){
//        //System.out.println(WxApiConfig.WXLOGIN_JSCODE2SESSION+"&js_code="+code);
////        String result= HttpClientUtil.doGet(WxApiConfig.WXLOGIN_JSCODE2SESSION+"&js_code="+code);
////        System.out.println(result);
////        JSONObject json = JSONObject(result);
////        System.out.println(json.get("openid"));
//        String openid=(new JSONObject(HttpClientUtil.doGet(WxApiConfig.WXLOGIN_JSCODE2SESSION+"&js_code="+code)));
//        QueryWrapper<VoteUser> qw=new QueryWrapper<>();
//        qw.eq("openid",openid);
//        VoteUser voteUser=voteUserService.getOne(qw);
//
//        if(voteUser!=null){
//            voteUser=new VoteUser();
//            voteUser.setOpenid(openid);
//            voteUser.setVuUserId(openid);
//            voteUser.setVuUserName("微信用户"+ UUID.randomUUID().toString().substring(0,10));
//            voteUser.setVuStatus(2);
//            voteUser.setVuVersion(1);
//            voteUser.setVuPassword("");
//            voteUserService.save(voteUser);
//        }
//        return voteUser;
//    }
}
